//
//  SettingReportViewController.swift
//  AIImage
//
//  Created by deng on 2023/12/10.
//

import UIKit

extension UITextView {
    func setPlaceholder(text: String, color: UIColor) {
        let placeholderLabel = UILabel()
        placeholderLabel.text = text
        placeholderLabel.textColor = color
        placeholderLabel.font = self.font
        placeholderLabel.sizeToFit()
        placeholderLabel.frame.origin = CGPoint(x: 5, y: 8)
        placeholderLabel.isHidden = !self.text.isEmpty
        self.addSubview(placeholderLabel)
        
        NotificationCenter.default.addObserver(forName: UITextView.textDidChangeNotification, object: nil, queue: nil) { [weak self] _ in
            self?.updatePlaceholderVisibility(placeholderLabel)
        }
    }
    
    private func updatePlaceholderVisibility(_ placeholderLabel: UILabel) {
        placeholderLabel.isHidden = !self.text.isEmpty
    }
}


class SettingReportViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        if #available(iOS 11.0, *) {
            UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
        
        view.backgroundColor = .hexString("#36393f")
        
        if #available(iOS 15.0, *) {
            let barAppearance = UINavigationBarAppearance.init()
            barAppearance.backgroundColor = .hexString("#24272e")
            navigationController?.navigationBar.scrollEdgeAppearance = barAppearance
            navigationController?.navigationBar.standardAppearance = barAppearance
        } else {
            navigationController?.navigationBar.barTintColor = .hexString("#24272e")
            navigationController?.navigationBar.shadowImage = UIImage()
            navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        }
        navigationController?.navigationBar.tintColor = .white
        navigationController?.navigationBar.isTranslucent = false
        
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "back_icon"), for: .normal)
        leftBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: leftBtn)
        leftBtn.addTarget(self, action: #selector(goBack), for: .touchUpInside)
        
        
        let reportView = UITextView.init()
        reportView.setPlaceholder(text: "填写建议和意见", color: .lightGray)
        view.addSubview(reportView)
        reportView.snp.makeConstraints { make in
            make.left.equalTo(20*DS.s)
            make.right.equalTo(-20*DS.s)
            make.top.equalTo(30*DS.s)
            make.height.equalTo(180*DS.s)
        }
        reportView.layer.cornerRadius = 5
        reportView.layer.masksToBounds = true
        
        let reportBtn = UIButton.init()
        reportBtn.setTitle("提交建议意见", for: .normal)
        reportBtn.titleLabel?.font = .systemFont(ofSize: 18, weight: .bold)
        reportBtn.setTitleColor(.white, for: .normal)
        reportBtn.backgroundColor = .hexString("#24272e")
        view.addSubview(reportBtn)
        reportBtn.snp.makeConstraints { make in
            make.top.equalTo(reportView.snp.bottom).offset(16*DS.s)
            make.left.right.equalTo(reportView)
            make.height.equalTo(50*DS.s)
        }
        reportBtn.addTarget(self, action: #selector(reportClick), for: .touchUpInside)
        reportBtn.layer.cornerRadius = 5
        reportBtn.layer.masksToBounds = true
        
        
        
    }
    
    @objc func reportClick(){
        
        let loadingView = LoadingView(frame: CGRect(x: KScreenWidth/2-50*DS.s, y: KScreenHeight/2-50*DS.s, width: 100*DS.s, height: 100*DS.s), text: "提交中...")
        view.addSubview(loadingView)
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
            loadingView.stopLoading()
            self.view.makeToast("提交成功")
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
                self.navigationController?.popViewController(animated: true)
            }
        }
    }
    
    @objc func goBack(){
        
        self.navigationController?.popViewController(animated: true)
    }


}
